﻿<UserControl x:Class="NthDownload.DownloadPlugins.Http.Views.HttpDownloadsAddView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:behaviors="clr-namespace:NthDownload.Common.Interactivity.Behaviors;assembly=NthDownload.Common"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"                          
             xmlns:validationrules="clr-namespace:NthDownload.Common.Validation.Rules;assembly=NthDownload.Common"
             xmlns:HttpPlugins="clr-namespace:NthDownload.DownloadPlugins.Http"
             xmlns:cal="http://www.caliburnproject.org"
             HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5,0,5,0"
             cal:Message.Attach="[Event Loaded] = [Action UILoaded]">
    <Grid cal:Message.Attach="[Event Unloaded] = [Action UIUnloaded]">
        <Grid.BindingGroup>
            <BindingGroup NotifyOnValidationError="True" Name="bg">
                <i:Interaction.Behaviors>
                    <behaviors:BindingGroupValidatorBinder BindingGroupValidator="{Binding BindingGroupValidator}" />
                </i:Interaction.Behaviors>
            </BindingGroup>
        </Grid.BindingGroup>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Text="Path" Style="{StaticResource Label}" Margin="0,0,5,0"/>
        <TextBox Grid.Row="0" Grid.Column="1" Style="{StaticResource MediumTextBox}" Margin="0,5,0,0" cal:Message.Attach="[Event KeyDown] = [Action KeyDown($eventArgs)]">
            <i:Interaction.Behaviors>
                <behaviors:FocusOnLoad />
            </i:Interaction.Behaviors>
            <TextBox.Text>
                <Binding Path="Path" BindingGroupName="bg">
                    <Binding.ValidationRules>
                        <validationrules:RequiredValidationRule />
                        <validationrules:UriValidationRule SupportedSchemas="http,https" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        <Button Grid.Row="0" Grid.Column="2" x:Name="RefreshFromSource" Content="Refresh" Margin="5,5,0,0" Style="{StaticResource StyleLessButton}" HorizontalAlignment="Left" />

        <TextBlock Grid.Row="1" Text="Destination Path" Style="{StaticResource Label}" Margin="0,5,5,0"/>
        <TextBox Grid.Row="1" Grid.Column="1" Style="{StaticResource MediumTextBox}" Margin="0,5,0,0">
            <TextBox.Text>
                <Binding Path="DestinationPath" BindingGroupName="bg">
                    <Binding.ValidationRules>
                        <validationrules:RequiredValidationRule />
                        <validationrules:UriValidationRule SupportedSchemas="file" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        <Button Grid.Row="1" Grid.Column="2" x:Name="ChangeDestination" Content="..." Margin="5,5,0,0" Style="{StaticResource StyleLessButton}" HorizontalAlignment="Left" />

        <TreeView Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3" Margin="0,5,0,0" ItemsSource="{Binding RootNodes}" ItemContainerStyle="{StaticResource StretchedTreeViewItems}">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type HttpPlugins:HttpNode}" ItemsSource="{Binding Children}">
                    <Grid HorizontalAlignment="Stretch">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Grid.Column="0">
                            <Button Content="..." cal:Message.Attach="[Event Click] = [Action LoadChildren($dataContext)]"  Margin="0,0,5,0" Style="{StaticResource StyleLessButton}" 
                                    HorizontalAlignment="Left" Visibility="{Binding IsLoadingChildren, Converter={StaticResource BooleanToVisiblityFalseVisible}}" />
                            <Button Content="X" cal:Message.Attach="[Event Click] = [Action CancelLoadChildren($dataContext)]"  Margin="0,0,5,0" Style="{StaticResource StyleLessButton}" 
                                    HorizontalAlignment="Left" Visibility="{Binding IsLoadingChildren, Converter={StaticResource BooleanToVisiblityTrueVisible}}"
                                    IsEnabled="{Binding CancelLoadingChildren, Converter={StaticResource BooleanInversionConverter}}"/>                            
                            <CheckBox IsChecked="{Binding IsSelected}" Margin="0,0,5,0" />
                            <TextBlock Text="{Binding Path}" />
                        </StackPanel>

                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="1">
                            <Button Content="Select Children" cal:Message.Attach="[Event Click] = [Action SelectChildrenRecursively($dataContext)]"  Margin="0,0,5,0" 
                                    Style="{StaticResource TreeViewMouseOverButton}" HorizontalAlignment="Left" IsEnabled="{Binding IsLoadingChildren, Converter={StaticResource BooleanInversionConverter}}"/>
                            <Button Content="Deselect Children" cal:Message.Attach="[Event Click] = [Action DeselectChildrenRecursively($dataContext)]"  Margin="0,0,5,0" Style="{StaticResource TreeViewMouseOverButton}" 
                                    HorizontalAlignment="Left" IsEnabled="{Binding IsLoadingChildren, Converter={StaticResource BooleanInversionConverter}}"/>
                        </StackPanel>      
                    </Grid>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>
        
        <StackPanel Grid.Row="3" Grid.ColumnSpan="4" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,5,0,0">
            <Button x:Name="Ok" Content="Ok" Margin="5,0,0,0" />
            <Button x:Name="Cancel" Content="Cancel" Margin="5,0,0,0" />
        </StackPanel>
    </Grid>
</UserControl>
